﻿using Microsoft.Win32;
using ServerBase.Client;
using ServerBase.Config;
using ServerBase.Other;
using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UtilLib;
using UtilPublic.ApiDispatcher;
using UtilPublic.Serialization;
using static UtilPublic.Serialization.Jsh;

namespace ToolGame
{
    /// <summary>
    /// PageGame.xaml 的交互逻辑
    /// </summary>
    public partial class PageGame : Page
    {
        public PageGame()
        {
            InitializeComponent();

            //DateTimePickerMailSendTime.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            //DateTimePickerMailSendTime.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            DateTimePickerMailSendTime.ValueDateTime = DateTime.Now;

            MainWindow.绑定控制器(this);
        }

        private void Button_Click_Public(object sender, RoutedEventArgs e) { App.WdMain.Button_Click_Public(sender, e); }

        #region 返回

        [ApiDpPubIA1((int)EGtAction.上传配置文件)]
        public void 上传配置文件(G2B_Gt_Action Rsp)
        {
            App.DebugInfo("上传配置文件 成功");
        }
        [ApiDpPubIA1((int)EGtAction.更新配置文件)]
        public void 更新配置文件(G2B_Gt_Action Rsp)
        {
            App.DebugInfo("更新配置文件 成功");
        }
        [ApiDpPubIA1((int)EGtAction.公告获取)]
        public void 公告获取(G2B_Gt_Action Rsp)
        {
            var GtArgs = Jsh.Jobj<CLS_GtNotice>(Rsp.GtArgs);
            TextBoxNoticeSystem.Text = GtArgs.NoticeSystem;
            TextBoxNoticeActivity.Text = GtArgs.NoticeActivity;
        }
        [ApiDpPubIA1((int)EGtAction.临时走马灯未发送列表)]
        public void 临时走马灯未发送列表(G2B_Gt_Action Rsp)
        {
            var GtArgs = Jsh.Jobj<Dictionary<long, CLS_GtNoticeRolling>>(Rsp.GtArgs);

            ComboBoxNoticeRollingRemove.ItemsSource = GtArgs.Keys;

            DataGridNoticeRollingList.BindingListObj(App.GetUserControlColumnNodeList<CLS_GtNoticeRolling>(), GtArgs.Values);
        }
        [ApiDpPubIA1((int)EGtAction.全局邮件列表)]
        public void 全局邮件列表(G2B_Gt_Action Rsp)
        {
            var GtArgs = Jsh.Jobj<Dictionary<long, CLS_GtMail>>(Rsp.GtArgs);

            ComboBoxMailGlobalRemove.ItemsSource = GtArgs.Keys;

            DataGridMailGlobalList.BindingListObj(App.GetUserControlColumnNodeList<CLS_GtMail>(), GtArgs.Values);
        }
        [ApiDpPubIA1((int)EGtAction.服务器限制登陆公告获取)]
        public void 服务器限制登陆公告获取(G2B_Gt_Action Rsp)
        {
            TextBoxRestrictLoginText.Text = Rsp.GtArgs;
        }
        #endregion

        #region 按钮
        #region 配置文件
        [ApiDpPubSA0("ButtonConfigFileSelect")]
        public void OButtonConfigFileSelect()
        {
            var fbd = new OpenFileDialog();
            fbd.InitialDirectory = Directory.GetCurrentDirectory();
            //是否支持多个文件的打开？
            fbd.Multiselect = false;
            //标题
            fbd.Title = "请选择配置文件";
            //文件类型
            fbd.Filter = "配置文件(*.json)|*.json";
            if (fbd.ShowDialog() == true)
            {
                //获取文件路径
                TextBoxConfigUploadPath.Text = fbd.FileName;
            }
        }
        [ApiDpPubSA0("ButtonConfigFileUpload")]
        public void OButtonConfigFileUpload()
        {
            var filePath = TextBoxConfigUploadPath.Text;
            if (File.Exists(filePath))
            {
                FileInfo fileInfo = new FileInfo(filePath);
                if (fileInfo.Length > 4096000)
                {
                    App.DebugError($"配置文件太大！大小{fileInfo.Length}");
                    return;
                }
                try
                {
                    FileStream fs_PTL = new FileStream(filePath, FileMode.Open);
                    StreamReader sr_PTL = new StreamReader(fs_PTL, Encoding.UTF8);

                    var text = sr_PTL.ReadToEnd();
                    if (text.IsNullOrEmpty())
                    {
                        text = "";
                    }
                    sr_PTL.Close();
                    var filename = System.IO.Path.GetFileName(filePath);

                    var GtArgs = new CLS_GtUploadConfig();
                    GtArgs.FileName = filename;
                    GtArgs.FileText = text;
                    var Req = App.GetReq(EGtAction.上传配置文件, Jsh.Jstr(GtArgs));
                    App.ClientNetSingle.Send(Req);
                }
                catch
                {
                    App.DebugError($"读取配置文件错误！文件{filePath}");
                    return;
                }
            }
            else
            {
                App.DebugError($"找不到配置文件！{filePath}");
                return;
            }
        }
        [ApiDpPubSA0("ButtonConfigConfInit")]
        public void OButtonConfigConfInit()
        {
            var Req = App.GetReq(EGtAction.更新配置文件);
            App.ClientNetSingle.Send(Req);
        }
        #endregion
        #region 公告
        [ApiDpPubSA0("ButtonNoticeGet")]
        public void OButtonNoticeGet()
        {
            var Req = App.GetReq(EGtAction.公告获取);
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonNoticeSet")]
        public void OButtonNoticeSet()
        {
            var GtArgs = new CLS_GtNotice()
            {
                NoticeSystem = TextBoxNoticeSystem.Text,
                NoticeActivity = TextBoxNoticeActivity.Text,
            };
            var Req = App.GetReq(EGtAction.公告设置, Jsh.Jstr(GtArgs));
            App.ClientNetSingle.Send(Req);
        }
        #endregion
        #region 临时走马灯
        [ApiDpPubSA0("ButtonNoticeRollingSend1")]
        public void OButtonNoticeRollingSend1()
        {
            var GtArgs = new CLS_GtNoticeRolling()
            {
                Uid = Util.GUID16(),
                Text = TextBoxNoticeRolling.Text,
                Count = (int)NumericUpDownNoticeRollingCount.VauleNumeric,
                TimeSend = DateTime.Now.ToString(),
            };
            if (GtArgs.Text.IsNullOrEmpty())
            {
                App.DebugError("请输入发送内容"); return;
            }
            var Req = App.GetReq(EGtAction.临时走马灯, Jsh.Jstr(GtArgs));
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonNoticeRollingSend2")]
        public void OButtonNoticeRollingSend2()
        {
            var GtArgs = new CLS_GtNoticeRolling()
            {
                Uid = Util.GUID16(),
                Text = TextBoxNoticeRolling.Text,
                Count = (int)NumericUpDownNoticeRollingCount.VauleNumeric,
                TimeSend = DateTimePickerNoticeRollingTime.ValueDateTime.ToString(),
            };
            if (GtArgs.Text.IsNullOrEmpty())
            {
                App.DebugError("请输入发送内容"); return;
            }
            var Req = App.GetReq(EGtAction.临时走马灯, Jsh.Jstr(GtArgs));
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonNoticeRollingList")]
        public void OButtonNoticeRollingList()
        {
            var Req = App.GetReq(EGtAction.临时走马灯未发送列表);
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonNoticeRollingRemove")]
        public void OButtonNoticeRollingRemove()
        {
            if (ComboBoxNoticeRollingRemove.SelectedItem != null)
            {
                var Req = App.GetReq(EGtAction.临时走马灯未发送删除, ComboBoxNoticeRollingRemove.SelectedItem.ToString());
                App.ClientNetSingle.Send(Req);
            }
        }
        #endregion
        #region 邮件
        [ApiDpPubSA0("ButtonMailSend")]
        public void OButtonMailSend()
        {
            var GtArgs = new CLS_GtMail() { Uid = Util.GUID16(), };
            //注册时间在发送邮件之前的 才能收到全服邮件
            //ID为双数的邮件 标记为无论是否在发邮件之前注册都能收到
            GtArgs.AllTimeReceive = (CheckBoxMailIsSendAllTime.IsChecked == true);

            if (CheckBoxMailIsGlobal.IsChecked == true)
            {
                GtArgs.MailType = EMailType.GM全服邮件;
            }
            else
            {
                GtArgs.MailType = EMailType.GM邮件;
                var textTarget = TextBoxMailReceiveIds.Text;
                GtArgs.ListTarget = textTarget.ToListLong("|");
                if (textTarget.IsNullOrEmpty()
                    || GtArgs.ListTarget.Count <= 0
                    || GtArgs.ListTarget[0] <= 0)
                {
                    App.DebugError("邮件对象 错误 指定玩家Id（邮件对象使用玩家Id，多个玩家使用|相隔，（例如： 7|12|123））全服邮件请勾选");
                    return;
                }
            }

            GtArgs.Title = TextBoxMailTitle.Text;
            if (GtArgs.Title.Length <= 0 || GtArgs.Title.Length >= 40)
            {
                App.DebugError("请输入有效的邮件标题(1-40个字符)");
                return;
            }

            GtArgs.Body = TextBoxMailBody.Text;
            if (GtArgs.Body.Length >= 300)
            {
                App.DebugError("邮件正文不要超过300个字符");
                return;
            }
            GtArgs.SenderName = TextBoxMailSenderName.Text;

            if (CheckBoxMailSendTime.IsChecked == true)
            {
                GtArgs.CreateTime = DateTimePickerMailSendTime.ValueDateTime.ToString();
            }
            else
            {
                GtArgs.CreateTime = DateTime.Now.ToString();
            }

            string textAttachments = TextBoxMailAtt.Text;
            var resultAttachments = textAttachments.ToListListInt();
            foreach (var nnn in resultAttachments)
            {
                if (nnn.Count < 3)
                {
                    continue;
                }
                var nnnn = new CLS_AwardItem()
                {
                    Type = (short)nnn[0],
                    ConfigId = nnn[1],
                    Count = nnn[2],
                };
                if (nnn.Count > 3)
                {
                    nnnn.Level = nnn[3];
                }
                nnnn.Count = Math.Max(nnnn.Count, 1);
                GtArgs.ListAttachments.Add(nnnn);
            }

            var Req = App.GetReq(EGtAction.发送邮件, Jsh.Jstr(GtArgs));
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonMailGlobalList")]
        public void OButtonMailGlobalList()
        {
            var Req = App.GetReq(EGtAction.全局邮件列表);
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonMailGlobalRemove")]
        public void OButtonMailGlobalRemove()
        {
            if (ComboBoxMailGlobalRemove.SelectedItem != null)
            {
                var Req = App.GetReq(EGtAction.删除全服邮件根据ID, ComboBoxMailGlobalRemove.SelectedItem.ToString());
                App.ClientNetSingle.Send(Req);
            }
        }
        #endregion
        #region 服务器登陆限制
        [ApiDpPubSA0("ButtonRestrictLoginTextGet")]
        public void OButtonRestrictLoginTextGet()
        {
            var Req = App.GetReq(EGtAction.服务器限制登陆公告获取);
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonRestrictLoginTextSet")]
        public void OButtonRestrictLoginTextSet()
        {
            var Req = App.GetReq(EGtAction.服务器限制登陆公告设置, TextBoxRestrictLoginText.Text.ToString());
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonRestrictLoginLock")]
        public void OButtonRestrictLoginLock()
        {
            var Req = App.GetReq(EGtAction.服务器登陆限制设置);
            App.ClientNetSingle.Send(Req);
        }
        [ApiDpPubSA0("ButtonRestrictLoginUnlock")]
        public void OButtonRestrictLoginUnlock()
        {
            var Req = App.GetReq(EGtAction.服务器登陆限制解除);
            App.ClientNetSingle.Send(Req);
        }
        #endregion
        #region 游戏临时功能
        #endregion
        #endregion


    }
}
